package com.itmayiedu.controller;

import com.itmayiedu.base.ReceiveXmlModel;
import com.itmayiedu.base.TextMessage;
import com.itmayiedu.utils.CheckUtil;
import com.itmayiedu.utils.ReceiveXmlProcess;
import com.itmayiedu.utils.WXBizMsgCrypt;
import com.itmayiedu.utils.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @Author cht 【wonderfor@vip.qq.com】
 * @Date 2018/10/25
 */
@RestController
@Slf4j
public class DispatCherController {

    //服务器验证接口地址
    @RequestMapping(value = "/dispatCher", method = RequestMethod.GET)
    public String dispatCher(String signature, String timestamp, String nonce, String echostr) {
        //1.验证参数
        boolean checkSignature = CheckUtil.checkSignature(signature, timestamp, nonce);
        //2.参数验证之后,返回随机数
        if(!checkSignature) {
            return null;
        }

        return echostr;
    }

    @RequestMapping(value = "/dispatCher", method = RequestMethod.POST)
    public void dispatCher(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String msg = null;
        //String signature = request.getParameter("signature");

        String msgSignature = request.getParameter("msg_signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // openid
        String openid = request.getParameter("openid");
        // 加密类型
        String encrypt_type = request.getParameter("encrypt_type");

        //3.根据类型进行处理返回值
        PrintWriter writer = response.getWriter();

        if (encrypt_type == null || encrypt_type.equals("raw")) { //不用加密
            msg = setText("蚂蚁课堂欢迎你","", "");
            writer.println(msg);
        }else{
            String s = XmlUtils.streamToString(request);
            WXBizMsgCrypt pc = new WXBizMsgCrypt("lpws2ekor5zjbroqju5bqajosxthepmo", "PbQgGEPtML8KoyBZK8CnqjARSKzqLeqoVO41fvB5gJn", "wxebab9a7d268e4b0e");
            String result2 = pc.decryptMsg(msgSignature, timestamp, nonce, s);
            System.out.println("解密后明文: " + result2);
            ReceiveXmlModel receiveXmlModel = ReceiveXmlProcess.getMsgEntity(result2);
            String text = null;
            if (receiveXmlModel.getContent().equals("大哥")) {
                text = "行行好啊";
            }else{
                text = "蚂蚁课堂欢迎你";
            }

            msg = setText(text,"gh_99a7c39a313d", "on1Wo1ed43kQx_5wPASrff3H2bEs");

            String mingwen = pc.encryptMsg(msg, timestamp, nonce);
            System.out.println("加密后明文: " + mingwen);
            writer.println(mingwen);
        }

        writer.close();
    }

    public String setText(String content, String fromUserName, String toUserName) {
        TextMessage textMessage = new TextMessage();
        textMessage.setContent(content);
        textMessage.setCreateTime(new Date().getTime());
        textMessage.setFromUserName(fromUserName);
        textMessage.setToUserName(toUserName);
        textMessage.setMsgType("text");
        //实体类转换为xml格式
        String msg = XmlUtils.messageToXml(textMessage);
        return msg;
    }
}
